package net.wangds.yunz.auth.handler;

import net.wangds.log.helper.LogHelper;
import net.wangds.utils.JsonUtils;
import net.wangds.web.entrypoint.BaseResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * 登陆失败的异常处理
 **/
@Component
public class LoginAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        /*
        if (e instanceof BadCredentialsException) {
            GlobalResultUtil.out(response, GlobalResultUtil.fail(ResultEnum.WRONG_LOGIN));//账号或密码错误
        } else if (e instanceof LockedException) {
            GlobalResultUtil.out(response, GlobalResultUtil.fail(ResultEnum.ACCOUNT_LOCKED));//账号被锁定
        } else if (e instanceof InternalAuthenticationServiceException) {
            GlobalResultUtil.out(response, GlobalResultUtil.fail(ResultEnum.ACCESS_FREQUENTLY));//接口限流
        } else {
        GlobalResultUtil.out(response, GlobalResultUtil.fail(ResultEnum.LOGIN_FAIL));//未知异常;排查原因
    }
    */

    String msg =authException.getLocalizedMessage();
    int code=-99;
    if(authException instanceof BadCredentialsException){
        msg = "账号或密码错误";
        code = -1;
    } else if (authException instanceof LockedException){
        msg = "账号被锁定";
        code = -2;
    } else if(authException instanceof AuthenticationServiceException){
        if(StringUtils.equals("验证码错误", msg)){
            code = -10;
        }
    }

    if(code==-99) {
        LogHelper.dev(() -> "身份认证失败!" + authException.getLocalizedMessage(), authException);
        msg = "未知错误";
        }
        try {
            //response.sendError(HttpStatus.UNAUTHORIZED.value(), authException.getLocalizedMessage());
            String body = JsonUtils.toJsonStr(BaseResp.of(null)
                    .msg(msg).code(code));
            response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            try(OutputStream os = response.getOutputStream()){
                os.write(body.getBytes(StandardCharsets.UTF_8));
                os.flush();
            }
        } catch (IOException e) {
            LogHelper.error(e);
        }
    }
}
